package com.bjh.service.impl;

import com.bjh.dao.SystemDao;
import com.bjh.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SystemServiceImpl implements SystemService {
    @Autowired
    private SystemDao systemDao;

    /**
     * 获取菜单列表
     * @return {@link List}
     * @throws Exception Exception
     */
    @Override
    public List<?> getMenus() throws Exception {
        return getMenusByParentId(0);
    }

    /**
     * 递归获取所有菜单列表
     * @param parentId 上一级id
     * @return {@link List}
     * @throws Exception Exception
     */
    private List<Map<String, Object>> getMenusByParentId(int parentId) throws Exception {
        List<Map<String, Object>> menuList = systemDao.getMenus(parentId);
        for (Map<String, Object> menu : menuList){
            int menuId = (int) menu.get("id");
            List<Map<String, Object>> menuListF = getMenusByParentId(menuId);
            if(menuListF.size() > 0){
                menu.put("list",menuListF);
            }
        }
        return menuList;
    }
}
